﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Baopx_API;

namespace Baopx.BackEnd.MangeAccount
{
    public partial class ManageUserRole : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            grdUserRole.DataSource = A.Controller.UserRoleController.GetAll();
            grdUserRole.DataBind();
        }

        protected void ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    UserRole userRole = new UserRole();
                    userRole.Name = ((TextBox) e.Item.FindControl("txtName")).Text;
                    userRole.Level = Convert.ToInt32(((TextBox)e.Item.FindControl("txtLevel")).Text);
                    userRole.IsActive = true;
                    A.Controller.UserRoleController.Insert(userRole);
                    break;
                case "cmdUpdate":
                    UserRole userRoleUpdate = A.Controller.UserRoleController.GetElementById(Convert.ToInt32(e.CommandArgument));
                    userRoleUpdate.Name = ((TextBox)e.Item.FindControl("txtName")).Text;
                    userRoleUpdate.Level = Convert.ToInt32(((TextBox)e.Item.FindControl("txtLevel")).Text);
                    userRoleUpdate.IsActive = ((CheckBox)e.Item.FindControl("chkActive")).Checked;
                    userRoleUpdate.IsActive = true;
                    A.Controller.UserRoleController.Update();
                    break;
                case "cmdDelete":
                    UserRole userRoleDelete =
                        A.Controller.UserRoleController.GetElementById(Convert.ToInt32(e.CommandArgument));
                    A.Controller.UserRoleController.Delete(userRoleDelete);
                    break;
            }
            BindData();
        }
    }
}